

package org.redrider.shop.domain

class WorkOrderController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ workOrderInstanceList: WorkOrder.list( params ), workOrderInstanceTotal: WorkOrder.count() ]
    }

    def show = {
        def workOrderInstance = WorkOrder.get( params.id )

        if(!workOrderInstance) {
            flash.message = "WorkOrder not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ workOrderInstance : workOrderInstance ] }
    }

    def delete = {
        def workOrderInstance = WorkOrder.get( params.id )
        if(workOrderInstance) {
            try {
                workOrderInstance.delete(flush:true)
                flash.message = "WorkOrder ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "WorkOrder ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "WorkOrder not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def workOrderInstance = WorkOrder.get( params.id )

        if(!workOrderInstance) {
            flash.message = "WorkOrder not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ workOrderInstance : workOrderInstance ]
        }
    }

    def update = {
        def workOrderInstance = WorkOrder.get( params.id )
        if(workOrderInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(workOrderInstance.version > version) {
                    
                    workOrderInstance.errors.rejectValue("version", "workOrder.optimistic.locking.failure", "Another user has updated this WorkOrder while you were editing.")
                    render(view:'edit',model:[workOrderInstance:workOrderInstance])
                    return
                }
            }
            workOrderInstance.properties = params
            if(!workOrderInstance.hasErrors() && workOrderInstance.save()) {
                flash.message = "WorkOrder ${params.id} updated"
                redirect(action:show,id:workOrderInstance.id)
            }
            else {
                render(view:'edit',model:[workOrderInstance:workOrderInstance])
            }
        }
        else {
            flash.message = "WorkOrder not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def workOrderInstance = new WorkOrder()
        workOrderInstance.properties = params
        return ['workOrderInstance':workOrderInstance]
    }

    def save = {
        def workOrderInstance = new WorkOrder(params)
        if(!workOrderInstance.hasErrors() && workOrderInstance.save()) {
            flash.message = "WorkOrder ${workOrderInstance.id} created"
            redirect(action:show,id:workOrderInstance.id)
        }
        else {
            render(view:'create',model:[workOrderInstance:workOrderInstance])
        }
    }
}
